﻿$(function() {
CheckNickName();
});

function CheckNickName() {
    $('.nicknametext').blur(function() {
        if ("" != this.value) {
            var arr = this.value.splitAlphabetAndOthers();
            var joined = arr.join("|");
            $.ajax({ type: 'POST',
                contentType: 'application/json',
                url: relRootPath + '/Services/Services.asmx/GetAbb',
                data: "{ 'input': '" + joined + "', 'splitter':'|', 'addSound':false }",
                dataType: 'json',
                success: function(result) {
                    ResolvePolyphone(arr, result.d);
                },
                error: function(result, status) {

                }
            }
                     );
        }
    });

    $('.nicknametext').focusin(function() {
        $('#polyphonebox > tbody:last').html("");
    });
}

function ResolvePolyphone(charArr, result) {
    var polyphone = false;
    var box = $('#polyphonebox > tbody:last');
    var hdnVal = "";
    for (var i = 0; i < result.length; i++) {
        var ppList = result[i][charArr[i]];
        if (ppList == null) {
            hdnVal += charArr[i];
            continue;
        }
        else if (ppList.length > 1) {
            polyphone = true;
            var tr = "<tr><td><strong>" + charArr[i] + "</strong>:</td><td>";
            for (var j = 0; j < ppList.length; j++) {
                tr += "<span class='ctn-p-r'><input value='" + i + ppList[j].substring(0, 1) + "' type='radio' " + ((j == 0) ? "checked='checked'" : "") + " name='ppo" + i + "' id='ppo" + i + j + "' /><label for='ppo" + i + j + "'>" + ppList[j] + "</label></span>";
            }
            tr += "</td></tr>";

            box.append(tr);
            hdnVal += "{" + i + "}";
        }
        else {
            hdnVal += ppList[0].substring(0, 1);
        }
    }

    $('.nicknamebox input:hidden').val(hdnVal);
    if (polyphone) {
        OpenJqueryDialog();
    }
}

function MakeAbb() {
    var hdnVal = $('.nicknamebox input:hidden');
    $('#polyphonebox input:radio:checked').each(function() {
        var arrIndex = $(this).val().substring(0, 1);
        var letter = $(this).val().substring(1);
        hdnVal.val(hdnVal.val().replace("{" + arrIndex + "}", letter));
    });

}